package org.jeecg.wms.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.qis.entity.QisProcdetailCfg;
import org.jeecg.qis.entity.QisProcheadValue;
import org.jeecg.wms.entity.WmsMatdetailCfg;
import org.jeecg.wms.entity.WmsMatheadCfg;
import org.jeecg.wms.service.IWmsMatdetailCfgService;
import org.jeecg.wms.vo.WmsMatdetailCfgVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags="来料检测配置明细")
@RestController
@RequestMapping("/wms/wmsMatdetailCfg")
@Slf4j
public class WmsMatdetailCfgController extends JeecgController<WmsMatdetailCfg, IWmsMatdetailCfgService> {

    @Autowired
    private IWmsMatdetailCfgService wmsMatdetailCfgService;

    @Autowired
    private CommonAPI commonAPI;

    /**
     * 来料检测配置明细分页列表查询--通过主表ID查询
     *
     * @param wmsMatdetailCfg
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="来料检测配置明细-通过主表ID查询", notes="来料检测配置明细-通过主表ID查询")
    @GetMapping(value = "/wmsMatdetailCfgByMainId")
    @DS("WMS")
    public Result<IPage<WmsMatdetailCfg>> wmsMatdetailCfgByMainId(WmsMatdetailCfg wmsMatdetailCfg,
                                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<WmsMatdetailCfg> queryWrapper = QueryGenerator.initQueryWrapper(wmsMatdetailCfg, req.getParameterMap());
        Page<WmsMatdetailCfg> page = new Page<WmsMatdetailCfg>(pageNo, pageSize);
        IPage<WmsMatdetailCfg> pageList = wmsMatdetailCfgService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 查询检验单子工序检验单
     *
     * @param headId
     * @return
     */
    @ApiOperation(value = "根据工单号获取工序列表", notes = "根据工单号获取工序列表")
    @GetMapping(value = "/initLeft")
    @DS("WMS")
    public Result<IPage<WmsMatdetailCfgVO>> initLeft(String headId) {
        Page<WmsMatdetailCfgVO> page = new Page<WmsMatdetailCfgVO>(1, 100);
        IPage<WmsMatdetailCfgVO> list = wmsMatdetailCfgService.getDetailListBycheckNo(page,headId);
        return Result.OK(list);
    }

    /**
     *   添加
     *
     * @param wmsMatdetailCfg
     * @return
     */
    @AutoLog(value = "来料检测配置明细-添加")
    @ApiOperation(value="来料检测配置明细-添加", notes="来料检测配置明细-添加")
    @PostMapping(value = "/add")
    @DS("WMS")
    public Result<String> add(@RequestBody WmsMatdetailCfg wmsMatdetailCfg) {
        if (StringUtils.isBlank(wmsMatdetailCfg.getAddress())) {
            wmsMatdetailCfg.setAddress("");
        }
        wmsMatdetailCfgService.save(wmsMatdetailCfg);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param wmsMatdetailCfg
     * @return
     */
    @AutoLog(value = "来料检测配置明细-编辑")
    @ApiOperation(value="来料检测配置明细-编辑", notes="来料检测配置明细-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    @DS("WMS")
    public Result<String> edit(@RequestBody WmsMatdetailCfg wmsMatdetailCfg) {
        if (StringUtils.isBlank(wmsMatdetailCfg.getAddress())) {
            wmsMatdetailCfg.setAddress("");
        }
        wmsMatdetailCfgService.updateById(wmsMatdetailCfg);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过选中对象删除
     *
     * @param wmsMatdetailCfg
     * @return
     */
    @AutoLog(value = "来料检测配置明细-通过id删除")
    @ApiOperation(value="来料检测配置明细-通过id删除", notes="来料检测配置明细-通过id删除")
    @RequestMapping(value = "/delete")
    @DS("WMS")
    public Result<String> delete(@RequestBody WmsMatdetailCfg wmsMatdetailCfg) {
        wmsMatdetailCfgService.removeById(wmsMatdetailCfg);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param list
     * @return
     */
    @AutoLog(value = "来料检测配置明细-批量删除")
    @ApiOperation(value="来料检测配置明细-批量删除", notes="来料检测配置明细-批量删除")
    @RequestMapping(value = "/deleteBatch")
    @DS("WMS")
    public Result<String> deleteBatch(@RequestBody List<WmsMatdetailCfg> list) {
        list.forEach(item->{
            wmsMatdetailCfgService.removeById(item);
        });
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "原材料主数据-通过id查询")
    @ApiOperation(value="来料检测配置明细-通过id查询", notes="来料检测配置明细-通过id查询")
    @GetMapping(value = "/queryById")
    @DS("WMS")
    public Result<WmsMatdetailCfg> queryById(@RequestParam(name="id",required=true) String id) {
        WmsMatdetailCfg wmsMatdetailCfg = wmsMatdetailCfgService.getById(id);
        if(wmsMatdetailCfg==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsMatdetailCfg);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsMatdetailCfg
     */
    //@RequiresPermissions("org.jeecg:wms_partdata_cfg:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("WMS")
    public ModelAndView exportXls(HttpServletRequest request, WmsMatdetailCfg wmsMatdetailCfg) {
        return super.exportXls(request, wmsMatdetailCfg, WmsMatdetailCfg.class, "来料检测配置明细");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @DS("WMS")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            long start = System.currentTimeMillis();
            List<WmsMatdetailCfg> list = ExcelImportUtil.importExcel(file.getInputStream(), WmsMatdetailCfg.class, params);
            try {
                for (WmsMatdetailCfg temp : list) {
                    wmsMatdetailCfgService.save(temp);
                }
                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
                return Result.OK("文件导入成功！数据行数：" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
        //return wmsMatdetailCfgService.importExcel(request, response, WmsMatdetailCfg.class);
    }
}
